package cn.yyh.serviceOrg.controller;

import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceOrg.entity.dto.LoginDTO;
import cn.yyh.serviceOrg.service.ILoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: LoginController
 * @Package: cn.yyh.user.controller
 * @date 2021/7/2
 */
@Api(tags = "登陆操作")
@RestController
@RequestMapping("/serviceOrg/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;


    @PostMapping("account")
    public R account(@RequestBody LoginDTO loginDTO) {
        try {
            Map<String,Object> map = loginService.accountLogin(loginDTO);
            return R.oK().data(map);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("weChat")
    public R weChatLogin(@RequestBody Map<String,Object> map){
        try {
            return loginService.weChatLogin(map);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("weChat/binder")
    public R weChatBinder(@RequestBody Map<String,Object> map) {
        try {
            Map<String,Object> m = loginService.weChatBinder(map);
            return R.oK().setResultObj(m);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage()).code(20000);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙").code(20000);
        }
    }

    @GetMapping("info")
    public R getInfo(){
        return R.oK().data("roles", "[admin]").data("name", "张三丰").data("avatar", "https://pics2.baidu.com/feed/b64543a98226cffcaa9bbee9f1799a96f703eab3.jpeg?token=99bafe934f8d5948be9b5cacfd50c5e5");
    }
}
